/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.admin;

import dto.BankDto;
import dto.TableDataModel;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import logic.BankLogicImpl;
import util.ValidateText;

/**
 *
 * @author khangpm
 */
public class AdminBank extends javax.swing.JPanel {

    /**
     * Creates new form AdminBank
     */
    int id;
    boolean flag = false;
    BankDto bank;
    BankLogicImpl logic;
    ArrayList<BankDto> list;
    TableDataModel model;
    StringTokenizer token;

    public AdminBank() {
        initComponents();
        bank = new BankDto();
        logic = new BankLogicImpl();
        list = new ArrayList<BankDto>();
        list = logic.getListBank();
        setTable();
        lockText();
        lockButton();
    }

    public void setTable() {
        ArrayList colHeader = new ArrayList();
        ArrayList dataTable = new ArrayList();
        ArrayList temp;
        String birthday, day, month, year;
        colHeader.add("ID");
        colHeader.add("Bank");
        colHeader.add("Decription");
        for (int i = 0; i < list.size(); i++) {
            temp = new ArrayList();
            temp.add(list.get(i).getBankID());
            temp.add(list.get(i).getBankName());
            temp.add(list.get(i).getDescribe());
            dataTable.add(temp);
        }
        model = new TableDataModel(dataTable, colHeader);
        tblListBank.setModel(model);
    }

    public void reLoadTable() {
        list = logic.getListBank();
        setTable();
        lockText();
        lockButton();
        txtBankName.setText("");
        txaDecription.setText("");
        flag=false;
        

    }

    public void lockButton() {
        btnDelete.setEnabled(false);
        btnEdit.setEnabled(false);
    }

    public void unLockButton() {
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
    }

    public void lockText() {
        txtBankName.setEditable(false);
        txaDecription.setEditable(false);
    }

    public void unlockText() {
        txtBankName.setEditable(true);
        txaDecription.setEditable(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtBankName = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txaDecription = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblListBank = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(750, 510));
        setPreferredSize(new java.awt.Dimension(750, 510));

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText("Name:");

        jLabel3.setText("Decription:");

        btnAdd.setText("Add New");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        txaDecription.setColumns(20);
        txaDecription.setRows(5);
        txaDecription.setWrapStyleWord(true);
        jScrollPane2.setViewportView(txaDecription);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(132, 132, 132)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtBankName, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(179, 179, 179)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(205, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBankName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnEdit)
                    .addComponent(btnDelete))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        tblListBank.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
            }
        ));
        tblListBank.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        tblListBank.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListBankMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblListBank);

        jLabel1.setFont(new java.awt.Font("Vani", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 153, 153));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Bank Management");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(163, 163, 163)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblListBankMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListBankMouseClicked
        unLockButton();
        txtBankName.setText(tblListBank.getValueAt(tblListBank.getSelectedRow(), 1).toString());
        txaDecription.setText(tblListBank.getValueAt(tblListBank.getSelectedRow(), 2).toString());
        id = Integer.valueOf(tblListBank.getValueAt(tblListBank.getSelectedRow(), 0).toString());

    }//GEN-LAST:event_tblListBankMouseClicked

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        int confirm = JOptionPane.showConfirmDialog(this, "Are you sure?");
        if (confirm == JOptionPane.YES_OPTION) {
            if (logic.deleteBank(id)) {
                JOptionPane.showMessageDialog(this, "Delete is successfully!");
                reLoadTable();
            } else {
                JOptionPane.showMessageDialog(this, "Delete is not successfully!");
            }

        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        if (btnEdit.getText().equals("Edit")) {
            btnAdd.setEnabled(false);
            btnDelete.setEnabled(false);
            btnEdit.setText("Update");
            txtBankName.setEditable(true);
            txaDecription.setEditable(true);
        } else {
            btnAdd.setEnabled(true);
            btnDelete.setEnabled(true);
            bank.setBankID(id);
            bank.setBankName(txtBankName.getText());
            bank.setDescribe(txaDecription.getText());
            /////////////////////////////
            BankLogicImpl bankLogic = new BankLogicImpl();
            if (bankLogic.updateBank(bank) == true) {
                JOptionPane.showMessageDialog(this, "Update is successfully!");
                reLoadTable();
            } else {
                JOptionPane.showMessageDialog(this, "Update is not successfully!");
            }
            /////////////////////////////

            txtBankName.setText("");
            txaDecription.setText("");
            btnEdit.setText("Edit");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (flag == false) {
            unlockText();
            txtBankName.setText("");
            txaDecription.setText("");
            btnDelete.setEnabled(false);
            btnEdit.setEnabled(false);
            flag=true;
        } else {

            if (ValidateText.checkNameAndDecript(txtBankName.getText())) {
                bank.setBankName(txtBankName.getText());
                bank.setDescribe(txaDecription.getText());
                if (logic.insertBank(bank)) {
                    JOptionPane.showMessageDialog(this, "Insert is successfully!");
                    reLoadTable();

                } else {
                    JOptionPane.showMessageDialog(this, "Insert is not successfully!");
                }
            } else {
                JOptionPane.showMessageDialog(this, "Invalid bank name!");
            }
        }


    }//GEN-LAST:event_btnAddActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblListBank;
    private javax.swing.JTextArea txaDecription;
    private javax.swing.JTextField txtBankName;
    // End of variables declaration//GEN-END:variables
}
